﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Site.Components
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Reflection;
    using Site.Components.Entity;

   
        public static class DataExtensions
        {
            /// <summary>
            /// DataRow扩展方法：将DataRow类型转化为指定类型的实体
            /// </summary>
            /// <typeparam name="T">实体类型</typeparam>
            /// <returns></returns>
            public static T ToModel<T>(this DataRow dr) where T : class, new()
            {
                return ToModel<T>(dr, true);
            }
            /// <summary>
            /// DataRow扩展方法：将DataRow类型转化为指定类型的实体
            /// </summary>
            /// <typeparam name="T">实体类型</typeparam>
            /// <param name="dateTimeToString">是否需要将日期转换为字符串，默认为转换,值为true</param>
            /// <returns></returns>
            /// <summary>
            public static T ToModel<T>(this DataRow dr, bool dateTimeToString) where T : class, new()
            {
                if (dr != null)
                    return ToList<T>(dr.Table, dateTimeToString).First();

                return null;
            }

            /// <summary>
            /// DataTable扩展方法：将DataTable类型转化为指定类型的实体集合
            /// </summary>
            /// <typeparam name="T">实体类型</typeparam>
            /// <returns></returns>
            public static List<T> ToList<T>(this DataTable dt) where T : class, new()
            {
                return ToList<T>(dt, true);
            }

            /// <summary>
            /// DataTable扩展方法：将DataTable类型转化为指定类型的实体集合
            /// </summary>
            /// <typeparam name="T">实体类型</typeparam>
            /// <param name="dateTimeToString">是否需要将日期转换为字符串，默认为转换,值为true</param>
            /// <returns></returns>
            public static List<T> ToList<T>(this DataTable dt, bool dateTimeToString) where T : class, new()
            {
                List<T> list = new List<T>();

                if (dt != null)
                {
                    List<PropertyInfo> infos = new List<PropertyInfo>();

                    Array.ForEach<PropertyInfo>(typeof(T).GetProperties(), p =>
                    {
                        if (dt.Columns.Contains(p.Name) == true)
                        {
                            infos.Add(p);
                        }
                    });

                    SetList<T>(list, infos, dt, dateTimeToString);
                }

                return list;
            }

            #region 私有方法

            private static void SetList<T>(List<T> list, List<PropertyInfo> infos, DataTable dt, bool dateTimeToString) where T : class, new()
            {
                foreach (DataRow dr in dt.Rows)
                {
                    T model = new T();

                    infos.ForEach(p =>
                    {
                        if (dr[p.Name] != DBNull.Value)
                        {
                            object tempValue = dr[p.Name];
                            if (dr[p.Name].GetType() == typeof(DateTime) && dateTimeToString == true)
                            {
                                tempValue = dr[p.Name].ToString();
                            }
                            try
                            {
                                p.SetValue(model, tempValue, null);
                            }
                            catch (Exception ex) { throw ex; }
                        }
                    });
                    list.Add(model);
                }
            }

            #endregion
        }


        public static class MergeList
        {
            public static IList<ProductInOutDetail> Merge(this IList<ProductInOutDetail> list)
            {
                var newlist = new List<ProductInOutDetail>();
                foreach (var item in list)
                {                    
                    if (newlist.Find(x => x.CheckNumber == item.CheckNumber) == null)
                    {
                        newlist.Add(item);
                    }
                    else
                    {
                        var entity = newlist.Find(x => x.CheckNumber == item.CheckNumber);

                        entity.电缆桥支架槽盒 += item.电缆桥支架槽盒;

                        entity.防火材料 += item.防火材料;

                        entity.钢材 += item.钢材;

                        entity.工器具 += item.工器具;

                        entity.耗材 += item.耗材;

                        entity.降阻剂 += item.降阻剂;

                        entity.金具 += item.金具;

                        entity.绝缘材料 += item.绝缘材料;

                        entity.汽配 += item.汽配;

                        entity.塔材及加工件 += item.塔材及加工件;

                        entity.线材 += item.线材;

                        entity.硬母线 += item.硬母线;

                        entity.主要设备 += item.主要设备;
                    }
                }

                return newlist;
            }



            public static IList<ProductOutStat> MergeStat(this IList<ProductOutStat> list)
            {
                var newlist = new List<ProductOutStat>();
                foreach (var item in list)
                {
                    if (newlist.Find(x => x.Name ==item.Name) == null)
                    {
                        newlist.Add(item);
                    }
                    else
                    {
                        var entity = newlist.Find(x=> x.Name == item.Name);

                        entity.电缆桥支架槽盒 += item.电缆桥支架槽盒;

                        entity.防火材料 += item.防火材料;

                        entity.钢材 += item.钢材;

                        entity.工器具 += item.工器具;

                        entity.耗材 += item.耗材;

                        entity.降阻剂 += item.降阻剂;

                        entity.金具 += item.金具;

                        entity.绝缘材料 += item.绝缘材料;

                        entity.汽配 += item.汽配;

                        entity.塔材及加工件 += item.塔材及加工件;

                        entity.线材 += item.线材;

                        entity.硬母线 += item.硬母线;

                        entity.主要设备 += item.主要设备;

                        entity.Billnumber += item.Billnumber;
                    }
                }

                return newlist;
            }
        }
}